﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DECOR.BL.BE;
using DECOR.DL.DALC.SAPC.Proxy;
using System.Data;

namespace DECOR.DL.DALC.SAPC
{
    public class AlmacenDALC : IAlmacenDALC
    {
        
       public static AlmacenDALC Instance() {
            if (_instance == null)
            {
                lock (typeof(AlmacenDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new AlmacenDALC();
                    }
                }
            }
            return _instance;
        }

        private AlmacenDALC() { }
        private static volatile AlmacenDALC _instance = null;


        #region IAlmacenDALC Members

        public List<GenericBE> listar(string pCodMaterial, string pCodCentro)
        {
            string charSep = ParametroDALC.getParamValueStatic("charSep");

            AlmacenesProxy proxy = new AlmacenesProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            //string ex_result = "";
            T001LTable tblResult = new T001LTable();
            WERKS p_werks = new WERKS();
            p_werks.Werks = pCodCentro;

            proxy.Zweb_Lista_Almacenes(pCodMaterial, p_werks, ref tblResult);
            //proxy.Zweb_Lista_Centros(out ex_result, ref tblResult);
            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            //proxy.Connection.Close();
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();

            List<GenericBE> listaResult = new List<GenericBE>();
            foreach (DataRow dr in dt.Rows)
            {
                GenericBE obj = new GenericBE();
                obj.Codigo = dr["lgort"].ToString();
                obj.Descripcion = (dr["WERKS"].ToString() != string.Empty ? dr["WERKS"].ToString() + charSep : "") + dr["lgort"].ToString() + charSep + dr["lgobe"].ToString();
                listaResult.Add(obj);
            }
            return listaResult;
        }

        #endregion
    }
}
